using System;
using System.Text;
using Oracle.ManagedDataAccess.Client;
using OracleInternal.Common;
using OracleInternal.Network;

namespace OracleInternal.TTC
{
	internal class TTCSessionGet : TTCFunction
	{
		internal const short SESSGET_TAG_MISMATCH = 1;

		internal const short SESSGET_PURITY_NEW = 2;

		internal const short SESSGET_SESSION_CHANGED = 4;

		internal const short SESSGET_STMTCACHE_DESTROY = 8;

		internal const short SESSGET_INFLAGS_MATCHANY = 1;

		private int m_c2sSessionGetflags;

		internal long m_s2cSessionGetflags;

		private string m_returnTag;

		internal TTCSessionGet(MarshallingEngine mEngine)
			: base(mEngine, 162, 0)
		{
			m_s2cSessionGetflags = 1L;
		}

		internal void GetSession(long c2sSessionFlags, bool bUseDRCPMultiTag)
		{
			if (ProviderConfig.m_bTraceLevelPrivate)
			{
				Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)131328, OracleTraceClassName.TTCSessionGet, OracleTraceFuncName.GetSession);
			}
			try
			{
				WriteFunctionHeader();
				m_c2sSessionGetflags = 0;
				if (bUseDRCPMultiTag)
				{
					m_c2sSessionGetflags |= 1;
				}
				m_marshallingEngine.MarshalDALC(null);
				m_marshallingEngine.MarshalPointer();
				m_marshallingEngine.MarshalPointer();
				if (m_marshallingEngine.NegotiatedTTCVersion >= 8)
				{
					m_marshallingEngine.MarshalUB2(m_c2sSessionGetflags);
					m_marshallingEngine.MarshalPointer();
					m_marshallingEngine.MarshalPointer();
				}
				ReceiveResponse();
			}
			finally
			{
				if (ProviderConfig.m_bTraceLevelPrivate)
				{
					Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)131584, OracleTraceClassName.TTCSessionGet, OracleTraceFuncName.GetSession);
				}
			}
		}

		internal void ReceiveResponse()
		{
			if (ProviderConfig.m_bTraceLevelPrivate)
			{
				Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)131328, OracleTraceClassName.TTCSessionGet, OracleTraceFuncName.ReceiveResponse);
			}
			bool flag = false;
			try
			{
				m_marshallingEngine.TTCErrorObject.Initialize();
				while (!flag)
				{
					try
					{
						switch (m_marshallingEngine.UnmarshalSB1())
						{
						case 23:
							ProcessServerSidePiggybackFunction(0);
							break;
						case 8:
							ReadRPAMessage();
							break;
						case 15:
							m_marshallingEngine.TTCErrorObject.ReadWarning();
							break;
						case 4:
							m_marshallingEngine.TTCErrorObject.ReadErrorMessage();
							flag = true;
							break;
						case 9:
							if (m_marshallingEngine.HasEOCSCapability)
							{
								m_marshallingEngine.m_endOfCallStatus = m_marshallingEngine.UnmarshalUB4();
							}
							if (m_marshallingEngine.HasFSAPCapability)
							{
								m_marshallingEngine.m_endToEndECIDSequenceNumber = m_marshallingEngine.UnmarshalUB2();
							}
							flag = true;
							break;
						default:
							throw new Exception("TTC Error");
						}
					}
					catch (NetworkException ex)
					{
						if (ex.ErrorCode != 3111)
						{
							throw;
						}
						m_marshallingEngine.m_oracleCommunication.Reset();
					}
					catch (Exception)
					{
						if (m_marshallingEngine.m_oraBufRdr != null)
						{
							m_marshallingEngine.m_oraBufRdr.ClearState();
						}
						m_marshallingEngine.m_oracleCommunication.Break();
						m_marshallingEngine.m_oracleCommunication.Reset();
					}
				}
			}
			catch (Exception ex3)
			{
				OracleException.HandleError(OracleTraceLevel.Private, (OracleTraceTag)268566528, OracleTraceClassName.TTCSessionGet, OracleTraceFuncName.ReceiveResponse, ex3);
				throw;
			}
			finally
			{
				if (ProviderConfig.m_bTraceLevelPrivate)
				{
					Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)131584, OracleTraceClassName.TTCSessionGet, OracleTraceFuncName.ReceiveResponse);
				}
			}
		}

		internal void ReadRPAMessage()
		{
			if (ProviderConfig.m_bTraceLevelPrivate)
			{
				Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)131328, OracleTraceClassName.TTCSessionGet, OracleTraceFuncName.ReadRPAMessage);
			}
			try
			{
				if (m_marshallingEngine.UnmarshalUB2() > 0)
				{
					short n = m_marshallingEngine.UnmarshalUB1();
					m_marshallingEngine.UnmarshalNBytes(null, 0, n);
				}
				m_s2cSessionGetflags = m_marshallingEngine.UnmarshalUB4();
				if (m_marshallingEngine.NegotiatedTTCVersion >= 8)
				{
					int num = m_marshallingEngine.UnmarshalUB2();
					if (num > 0)
					{
						byte[] bytes = m_marshallingEngine.UnmarshalNBytes(num);
						m_returnTag = Encoding.ASCII.GetString(bytes);
					}
				}
			}
			finally
			{
				if (ProviderConfig.m_bTraceLevelPrivate)
				{
					Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)131584, OracleTraceClassName.TTCSessionGet, OracleTraceFuncName.ReadRPAMessage);
				}
			}
		}
	}
}
